﻿Public Class ViewEmp


    Private Sub ViewEmployee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(DataGridView1)
        loaddata()
    End Sub

    Private Sub loaddata()
        cmd.Parameters("@search") = "%" & txtsearch.Text & "%"
        DataGridView1.DataSource = cmd.ExecuteDataTable("SELECT e.*,s.sex,ep.emp_pos_name FROM employee_tbl e INNER JOIN sex_tbl s on s.sex_id=e.sex_id INNER JOIN employee_position_tbl ep on ep.emp_pos_id=e.emp_pos_id WHERE e.Active=1 AND e.emp_name+e.emp_no like @search")
    End Sub

    Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        loaddata()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim frm As New NewEmp
        'frm.ShowDialog()
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            loaddata()
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = Me.DataGridView1.SelectedRows(0)
        Dim frm As New NewEmp
        frm.l = row.Cells(0).Value.ToString
        frm.txtno.Text = row.Cells(1).Value.ToString
        frm.txtname.Text = row.Cells(2).Value.ToString
        frm.dtpdob.Text = row.Cells(3).Value.ToString
        frm.txtpob.Text = row.Cells(4).Value.ToString
        frm.txtsalary.Text = row.Cells(5).Value.ToString
        frm.txtadr.Text = row.Cells(6).Value.ToString
        frm.txtphone.Text = row.Cells(7).Value.ToString
        frm.se = row.Cells(8).Value.ToString
        frm.pos = row.Cells(9).Value.ToString
        frm.txtmail.Text = row.Cells("mail").Value.ToString

        'If IsDBNull(row.Cells(12).Value) Then
        '    frm.PictureBox1.Image = Nothing
        'Else
        '    Dim bytImage() As Byte
        '    bytImage = CType(row.Cells(12).Value, Byte())
        '    Dim ms As New System.IO.MemoryStream(bytImage)
        '    Dim bmImage As New Bitmap(ms)
        '    ms.Close()
        '    frm.PictureBox1.Image = bmImage
        '    frm.PictureBox1.Refresh()
        'End If

        If row.Cells(12).Value Is DBNull.Value Then
            frm.PictureBox1.Image = Nothing
        Else
            Dim bytes As Byte() = DirectCast(row.Cells(12).Value, Byte())
            Dim m As New System.IO.MemoryStream(bytes)
            frm.PictureBox1.Image = New System.Drawing.Bitmap(m)
        End If

        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loaddata()
        End If
    End Sub

    Private Sub DataGridView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
        'Dim row = Me.DataGridView1.SelectedRows(0)
        'Dim frm As New NewEmp
        'MessageBox.Show(row.Cells(0).Value.ToString)
        'MessageBox.Show(row.Cells(1).Value.ToString)
        'MessageBox.Show(row.Cells(2).Value.ToString)
        'MessageBox.Show(row.Cells(3).Value.ToString)
        'MessageBox.Show(row.Cells(4).Value.ToString)
        'MessageBox.Show(row.Cells(5).Value.ToString)
        'MessageBox.Show(row.Cells(6).Value.ToString)
        'MessageBox.Show(row.Cells(7).Value.ToString)
        'MessageBox.Show(row.Cells(8).Value.ToString)
        'MessageBox.Show(row.Cells(9).Value.ToString)
        Button3_Click(sender, e)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'MessageBox.Show("មិនទាន់ធ្វើ")
        If Not MessageBox.Show("តើអ្នកពិតជាចង់លុបមែនទេ?") = Windows.Forms.DialogResult.OK Then
            Exit Sub
        End If
        If Me.DataGridView1.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = Me.DataGridView1.SelectedRows(0)
        cmd.Parameters("@id") = row.Cells(0).Value
        cmd.ExecuteNonQuery("UPDATE Employee_tbl set Active=0 WHERE emp_id=@id")
        MessageBox.Show("Delete Success")
        loaddata()
    End Sub
End Class